package com.hup.restTimer.view.rest;

import com.hup.restTimer.model.AppConfig;
import com.hup.restTimer.service.ImageService;
import com.hup.restTimer.service.MainService;
import com.hup.restTimer.view.main.MainWindow;
import com.hup.utils.commons.start.LookThemeUtil;
import com.hup.utils.commons.start.SpringContext;
import com.hup.utils.commons.start.StartupInfo;
import com.hup.utils.swing.FrameUtil;
import com.hup.utils.swing.FrameUtil.Gravity;
import com.hup.utils.swing.ImageUtil;
import com.hup.utils.swing.frame.BaseJWindow;
import com.hup.utils.swing.frame.SingletonWindowHelper;
import com.hup.utils.swing.frame.WindowEdgeHideHelper;
import com.hup.utils.swing.frame.WindowEdgeHideHelper.EdgeHideWindow;
import com.hup.utils.swing.frame.WindowLocationAdjustor;
import com.hup.utils.swing.frame.WindowLocationAdjustor.WindowLocationAdjustorCallback;
import com.hup.utils.swing.model.BaseWindowConfig;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.awt.Component;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

/**
 * [休息功能]浮窗
 * .悬浮,自动隐藏,可拖拽
 * .按钮:开始休息,停止休息
 * .调整背景图
 *
 * @author hugan
 * @date 2022/10/1
 */
@Slf4j
public class RestWindow extends BaseJWindow implements EdgeHideWindow {

    private final AppConfig config;
    @Getter
    private WindowEdgeHideHelper edgeHideHelper;
    private RestWindowForm form;

    public RestWindow(AppConfig config) {
        super(StartupInfo.getApplicationChName());
        this.config = config;

        initView();
        initListener();
    }

    private void initView() {
        form = new RestWindowForm();
        setContentPane(form.root);
        setAlwaysOnTop(true);

        form.btnRestStop.setVisible(false);
        LookThemeUtil.initFrameSize(this, 180, 50);

        //配置
        BaseWindowConfig windowConfig = config.restWindowConfig;
        if (windowConfig.loc == null) {
            FrameUtil.setLocation(this, Gravity.TOP_CENTER);
        } else {
            setLocation(windowConfig.loc);
        }
    }

    private void initListener() {
        WindowLocationAdjustor.bind(new LocationHandler());
        edgeHideHelper = WindowEdgeHideHelper.bind(this);

        ActionListener restListener = e -> {
            boolean isStart = form.btnRestStart == e.getSource();
            form.btnRestStart.setVisible(!isStart);
            form.btnRestStop.setVisible(isStart);
            SpringContext.getBean(MainService.class).switchRest(isStart);
        };
        form.btnRestStart.addActionListener(restListener);
        form.btnRestStop.addActionListener(restListener);
        form.btnImageChange.setActionListener(e -> SpringContext.getBean(ImageService.class)
                .doChangeImage(SingletonWindowHelper.getWindow(MainWindow.class)));
        form.btnImagePenetration.setActionListener(e -> {
            config.imagePenetration = form.btnImagePenetration.isSelected();
            SpringContext.getBean(MainService.class).updateWindowStyle(false);
            config.save();
        });
        form.slImageAlpha.addChangeListener(e -> {
            config.imageAlpha = form.slImageAlpha.getValue();
            MainWindow window = SingletonWindowHelper.getWindow(MainWindow.class);
            BufferedImage image = window.getBackgroundLabel().getIconImage();
            if (image != null) {
                ImageUtil.updateAlpha(image, config.imageAlpha);
                window.getBackgroundLabel().repaint();
            }
            boolean adjusting = form.slImageAlpha.getValueIsAdjusting();
            if (!adjusting) {
                config.save();
            }
        });
    }

    @Override
    public void setVisible(boolean b) {
        if (b) updateState();
        super.setVisible(b);
    }

    public void updateState() {
        form.btnRestStart.setVisible(!config.isResting);
        form.btnRestStop.setVisible(config.isResting);
        //
        boolean showImagePanel = config.imageShow && config.isResting;
        form.pImage.setVisible(showImagePanel);
        LookThemeUtil.initFrameSize(this, 180, !showImagePanel ? 50 : 68);
        if (showImagePanel) {
            form.slImageAlpha.setValue(config.imageAlpha);
            form.btnImagePenetration.setSelected(config.imagePenetration);
        }
    }

    private class LocationHandler implements WindowLocationAdjustorCallback {

        @Override
        public Window getWindow() {
            return RestWindow.this;
        }

        @Override
        public Component[] getAdjustor() {
            return new Component[]{form.root};
        }

        @Override
        public void afterMoved(int x, int y, Boolean onTop, Boolean onLeft) {
            if (config.restWindowConfig.loc == null) config.restWindowConfig.loc = new Point();
            config.restWindowConfig.loc.x = x;
            config.restWindowConfig.loc.y = y;
            log.info("loc={},{}", x, y);
            config.save();
        }

    }

}
